//
//  HLNetRequestPlugin.swift
//  SwiftPods
//
//  Created by hulinhu on 2018/1/18.
//  Copyright © 2018年 hulinhu. All rights reserved.

//  loading 图

import UIKit
import Moya
import Result

// 网络
let netWorkActivityPlugin = NetworkActivityPlugin { (change ,target) -> () in
    switch(change){
    case .ended:
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
    case .began:
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }
}

// 弹窗
public final class HLNetRequestPlugin: PluginType {
    public func willSend(_ request: RequestType, target: TargetType) {}
    public func didReceive(_ result: Result<Response, MoyaError>, target: TargetType) {
        
        switch result {
        case .success(let response):
            let json:Dictionary? = try! JSONSerialization.jsonObject(with: response.data,                                                                     options:.allowFragments) as! [String: Any]
            print(json as Any)
            HLProgressHUD.showSuccess("加载成功")
         case .failure:
            HLProgressHUD.showError("加载失败")
            break
        }
    }
}
